﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;
using System.IO;
using MySql.Data;
using MySql.Data.MySqlClient;
using OneMake.Excel;
using OneMake.Model;
using OneMake.DataAccess;
using PetaPoco;

namespace OneMake.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            testReport();
            Console.ReadLine();
        }
        static void testReport()
        {
            ReportHelper rh = new ReportHelper();
            //List<report_yyrb> reports = rh.get_report_yyrb(0, DateTime.Now.AddMonths(-1), DateTime.Now);
            //List<report_hdxg> hdxq = rh.get_report_hdxg(0, DateTime.Now.AddMonths(-1), DateTime.Now);
            //Console.WriteLine(reports.Count);
            //Console.WriteLine(hdxq.Count);
        }
        static void testPagination()
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            var result = db.Page<mf_m_qwatydf>(1, 20, "SELECT * FROM mf_m_qwat");
            foreach (var row in result.Items)
            {
                Console.WriteLine("{0} - {1}", row.oid, row.product);
            }
            //Console.WriteLine(result.TotalPages, result.CurrentPage, result)
        }
        static void testFieldMap()
        {
            DataHelper dh = new DataHelper();

            Dictionary<string, string> attrs = dh.GetTableFieldMap("hdp_d_stat");

            foreach (KeyValuePair<string, string> item in attrs)
                Console.WriteLine(item.Key + ":" + item.Value);            
        }
        static void testAttr<T>()
        {
            Type t = typeof(T);
            var a = t.GetCustomAttributes(typeof(TableNameAttribute), true);
            var b = t.GetCustomAttributes(typeof(PrimaryKeyAttribute), true);
            Console.WriteLine(a[0].ToString());
            Console.WriteLine(b[0].ToString());
        }
        public static int ValidEmployee(string username, string password)
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            employee_info empolyeeInfo = db.FirstOrDefault<employee_info>("select * from employee_info where employee_no=@0", username);
            if (empolyeeInfo != null)
            {
                if (empolyeeInfo.password == password)
                    return empolyeeInfo.employeeid;
            }
            return 0;
        }
        static void TestPeta()
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            foreach (var a in db.Query<ct_d_jxdb>("SELECT * FROM ct_d_jxdb"))
            {
                Console.WriteLine("{0} - {1}", a.oid, a.n_inquire);
            }

        }

        static void TestReadModel()
        {
            string filePath = @"E:\myprojects\OneMake\appsforsmes\onemake\App\OneMake.Test\xls\2011-07-23赤兔客服绩效数据.xlsx";

            ExcelHeleper helper = new ExcelHeleper();
            string sheetName = helper.GetSheetName(filePath, 0);
            //IDataReader reader = helper.ExecuteXlsReader(filePath, sheetName);

            //DataHelper ctHelper = new DataHelper();
            //ctHelper.ImportData(null, null);
        }

        static void TestAttribute()
        {
            Type type = typeof(ct_d_jxdb);
            
            //Querying Class-Field (only public) Attributes
            MapToExcelAttribute mapToAttr;
            foreach (PropertyInfo info in type.GetProperties())
            {
                foreach (Attribute attr in info.GetCustomAttributes(true))
                {
                    mapToAttr = attr as MapToExcelAttribute;
                    if (null != mapToAttr)
                    {
                        Console.WriteLine("Description of {0}:\n{1}", info.Name, mapToAttr.ColumnName);
                    }
                }
            }
        }

        static void TestExcel()
        {
            string filePath = @"..\..\xls\test.xlsx";

            ExcelHeleper helper = new ExcelHeleper();
            string sheetName = helper.GetSheetName(filePath, 0);
            //PrintDataReader(helper.ExecuteXlsReader(filePath, sheetName));
            //PrintDataSet(helper.ExecuteXlsDataSet(filePath, sheetName));
        }

        static void PrintDataSet(DataSet ds)
        {
            foreach (DataTable dt in ds.Tables)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    foreach (DataColumn dc in dt.Columns)
                    {
                        Console.WriteLine("{0},   {1},   {2} ", dt.TableName, dc.ColumnName, dr[dc]);
                    }
                    Console.WriteLine("\n");
                }

            }
        }

        static void PrintDataReader(IDataReader reader)
        {
            while (reader.Read())
            {
                Console.WriteLine(reader[1]);
            }
        }
        static void TestMySql()
        {
            string connStr = "server=localhost;user=web;database=onemake;port=3306;password=web;";
            MySqlConnection conn = new MySqlConnection(connStr);
            try
            {
                Console.WriteLine("Connecting to MySQL...");
                conn.Open();
                string sql = @"select * from ct_d_ia";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                IDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Console.Write(reader["n_ordered_amount"].ToString());
                    Console.WriteLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            conn.Close();
            Console.WriteLine("Done.");
        }

        static void GetAllExcelSheetDetail(string path)
        {
            ArrayList al = new ArrayList();
            GetAllDirList(path, al);

            ExcelHeleper helper = new ExcelHeleper();
            foreach(string fileName in al)
            {
                try
                {
                    string sheetName = helper.GetSheetName(fileName, 0);
                    //IDataReader reader = helper.ExecuteXlsReader(fileName, sheetName);
                    Console.WriteLine(fileName);

                    string columns = "";
                    //for (int i = 0; i < reader.FieldCount; i++)
                    //    columns += reader.GetName(i) + "  ";

                    Console.WriteLine(columns);
                    Console.WriteLine("\n");
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.Print(ex.Message);
                    //ignore
                }
            }
        }

        public static void GetAllDirList(string strBaseDir, ArrayList al)
        {
            DirectoryInfo di = new DirectoryInfo(strBaseDir);//获得目录信息

            string[] filters = new string[] {"*.csv", "*.xls", "*.xlsx" }; 
            foreach (string filter in filters)
            {
                //获得该目录下的指定类型的文件;
                FileInfo[] fiA = di.GetFiles(filter);
                for (int j = 0; j < fiA.Length; j++)
                    al.Add(fiA[j].FullName);
            }


            //子目录递归
            DirectoryInfo[] diA = di.GetDirectories();//获得该目录下的所有子目录
            for (int i = 0; i < diA.Length; i++)
            {
                GetAllDirList(diA[i].FullName, al);
            }
        }

    }
}
